﻿#pragma once

#include <iostream>

//! 调试类
class Debuger
{
	Debuger();

public:
	static Debuger* getInstance() { static Debuger d; return &d; }
	~Debuger();
	bool init();
	void setErrState();
	void setInforState();
	void setWarningState();
	void destory();
private:
	void* m_handle;
	FILE* m_in;
	FILE* m_out;
	FILE* m_err;
	bool m_isInit;
};


#ifdef _DEBUG

#define _L(x) __L(x)
#define __L(x) L##x

#define DebugErr(fmt,...) do																				\
{																											\
	Debuger::getInstance()->setErrState();																	\
	wprintf_s(L"[Err] funtion-> %ls ,file-> %ls At %d line\n", _L(__FUNCTION__),_L(__FILE__), __LINE__);	\
	wprintf_s(L##fmt,__VA_ARGS__ );																			\
	wprintf_s(L"\n");																						\
} while (0)

#define DebugInfor(fmt,...)do																				\
{																											\
	Debuger::getInstance()->setInforState();																\
	wprintf_s(L"[Infor] ");																					\
	wprintf_s(L##fmt, __VA_ARGS__);																			\
	wprintf_s(L"\n");																						\
} while (0)

#define DebugWarning(fmt, ... )do 																			\
{																											\
	Debuger::getInstance()->setWarningState();																\
	wprintf_s(L"[Warning] function-> %ls\n",_L(__FUNCTION__));												\
	wprintf_s(L##fmt, __VA_ARGS__);																			\
	wprintf_s(L"\n");																						\
} while (0)

#define EnsureWarning(v, fmt, ... )do 																			\
{																													\
	if(!(v))																								\
	{																															\
		Debuger::getInstance()->setWarningState();																\
		wprintf_s(L"[Warning] function-> %ls\n",_L(__FUNCTION__));												\
		wprintf_s(L##fmt, __VA_ARGS__);																			\
		wprintf_s(L"\n");	\
	}																											\
} while (0)

#define DebugerAssert(v, err_info, ...) do																		\
{																											\
	if (!(v))																								\
	{																										\
		DebugErr(err_info, __VA_ARGS__);																				\
	}																										\
	assert((v)&&(err_info));																				\
} while (0)

#else
#define DebugErr(fmt, ... ) 
#define DebugInfor(fmt , ...)
#define DebugWarning(fmt, ...) 
#define DebugerAssert(v,err_info)
#endif // _DEBUG 


